<template>
  <view>
    <!-- 搜索区域 -->
    <view class="search-box">
      <uni-search-bar  @input="input" :radius="80" cancelButton="none"></uni-search-bar>
    </view>
    
    <!-- 搜索列表区域-->
    <view class="suggest_list" v-if='searchList.length !== 0'>
      <view class="suggest_item" v-for="(item,i) in searchList" :key='i' @click="gotoDetail(item.goods_id)">
        <view class="good_name">{{item.goods_name}}</view>
        <uni-icons custom-prefix="iconfont" type="right" size="16"></uni-icons>
      </view> 
    </view>
    <!-- 搜索历史区域 -->
    <view class="history-box" v-else>
      <view class="history-title">
        <text>历史搜索</text>
        <uni-icons type="trash" @click="clear()" size="17"></uni-icons>
      </view>
      <view class="histort-list">
        <uni-tag  @click="gotoGoods_detail(item)" :text="item" v-for="(item, i) in historys" :key='i'></uni-tag>
      </view>
      
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        timer:null,
        kw:'',
        searchList:[],
        historyList:['a','b','c']
      };
    },
    onLoad(){
      // console.log(JSON.parse(|| '[]'))
      // console.log(JSON.parse(uni.getStorageSync('kw'))||'[]')
      this.historyList = JSON.parse(uni.getStorageSync('kw'))||'[]'
    },
    methods:{
      input(e){
        clearTimeout(this.timer)
        this.timer = setTimeout(()=>{
            this.kw = e
            this.getSearchList()
        },500)
      },
      async getSearchList(){
        if (this.kw.length === 0) {
          this.searchList = []
          return
        }
        const  {data:res} = await uni.$http.get('/api/public/v1/goods/qsearch',{query:this.kw})
        if (res.meta.status !== 200) return
          this.searchList = res.message
          this.saveSearchHistory()
        
      },
      gotoDetail(goods_id){
        console.log('s')
        uni.navigateTo({
          url:'/subpkg/goods_detail/goods_detail?goods_id='+goods_id
        })
      },
       saveSearchHistory(){
         // this.historyList.push(this.kw)
         // 数组转集合
         const set = new Set(this.historyList)
         // 删除原来的kw
         set.delete(this.kw)
         // 写入现有的kw
         set.add(this.kw)
         this.historyList = Array.from(set)
         // 对搜索历史持久化
         uni.setStorageSync('kw',JSON.stringify(this.historyList))
       },
       clear(){
         this.historyList = []
         uni.setStorageSync('kw','[]')
       },
       gotoGoods_detail(kw){
         uni.navigateTo({
           url:'/goods_detail/goods_detail?query='+kw
         })
       }
    },
    computed:{
      historys(){
        return [...this.historyList].reverse()
      }
    }
  }
</script>

<style lang="scss">
  .search-box{
    position: sticky;
    top: 0;
    z-index: 999;
  }
  
  .suggest_list{
    padding: 0 5px;
     .suggest_item{
       padding: 13px 0;
       font-size: 12px;
       border-bottom: 1px solid #8F8F94;
       display: flex;
       align-items: center;
       justify-content: space-between;
       .good_name{
         // 不换行
         white-space: nowrap;
         // 溢出部分隐藏
        overflow: hidden;
        // 显示小圆点
        text-overflow: ellipsis;
        margin-right: 3px;
       }
     }
  }
  
  .history-box{
    margin: 0 5px;
    .history-title{
      display: flex;
      justify-content: space-between;
      align-items: center;
      height: 40px;
      font-size: 13px;
      border-bottom: 1px solid #efefef;
    }
    .histort-list{
      display: flex;
      flex-wrap: wrap;
      .uni-tag{
        margin-left: 5px;
        margin-right: 5px;
      }
    }
  }
</style>
